package com.example.textview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.textview.util.Imytnterface;
import com.example.textview.util.Test;

import java.util.ArrayList;

public class ButtonActivity extends AppCompatActivity {

    private Button mBTN3;
    private TextView mTV1;
    Test test;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        mBTN3=findViewById(R.id.btn_3);
        mBTN3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ButtonActivity.this,"Button3被点击了",Toast.LENGTH_SHORT).show();
            }
        });

        mTV1=findViewById(R.id.tv_1);
        mTV1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ButtonActivity.this,"TV1被点击了",Toast.LENGTH_SHORT).show();

                test.test1();
            }
        });


        test=new Test();
        test.setImytnterface(new Imytnterface() {
            @Override
            public void callback(String msg, String adr) {

              // Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
                Log.d("EEEEEEEEEEE","22222222222222");

                Message message=new Message();
                message.what=1;

                handler.sendMessage(message);
            }


        });


        test.myThreadSave.mylist=new ArrayList<>();
        test.myThreadSave.mylist.add(1);
        test.myThreadSave.mylist.add(1);
        test.myThreadSave.mylist.add(1);
        test.myThreadSave.mylist.add(1);

        test.myThreadSave.setImytnterface(new Imytnterface() {
            @Override
            public void callback(String msg, String adr) {


                Message message=new Message();
                message.what=1;
                Bundle bundle=new Bundle();
                bundle.putString("MSG",msg);
                message.setData(bundle);
                handler.sendMessage(message);
            }
        });

        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        // ToastUtil.showMsg(HandlerActivity.this,"线程通讯成功！");


                        Log.d("Thread()","finish");

                        Bundle bundle=msg.getData();
                      String str=  bundle.getString("MSG");

                        Toast.makeText(getApplicationContext(), "线程通讯成功:  "+str, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    }


    public void showToast(View view)
    {
        Toast.makeText(this,"Button4我被点击了",Toast.LENGTH_SHORT).show();
    }
}